package vip.xumy.core.exception;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import lombok.extern.log4j.Log4j2;
import vip.xumy.core.pojo.com.AjaxResponse;

/**	All rights reserved
 *	author:mengyxu
 *	date:2019年6月4日
 */

@Log4j2
@ControllerAdvice
public class ControllerExceptionHandler {
	
	@ResponseBody
	@ExceptionHandler(CoreException.class)
	public AjaxResponse handlerSprtException(CoreException e) {
		log.warn(e.getMessage());
		return new AjaxResponse(false, e.getMessage());
	}
	
//	@ResponseBody
//	@ExceptionHandler(DuplicateKeyException.class)
//	public AjaxResponse handlerDuplicateKeyException(DuplicateKeyException e) {
//		return new AjaxResponse(false, "数据已存在，请勿重复添加");
//	}
	
	@ResponseBody
	@ExceptionHandler(SQLException.class)
	public AjaxResponse handlerSqlException(SQLException e) {
		log.warn(e.getMessage());
		return new AjaxResponse(false, "数据库访问出错！");
	}

	@ResponseBody
	@ExceptionHandler(NullPointerException.class)
	public AjaxResponse handlerNullPointerException(NullPointerException e) {
		log.warn(e.getMessage());
		return new AjaxResponse(false, "操作失败,缺少必要参数！");
	}

	@ResponseBody
	@ExceptionHandler(Exception.class)
	public Map<String, Object> handlerException(Exception e) {
		log.error("请求失败！",e);
		Map<String, Object> map = new HashMap<>();
		map.put("success", false);
		map.put("message", "操作失败，参数错误！");
		map.put("error", e.getMessage());
		return map;
	}

}
